//
//  MovieViewController.m
//  结构
//
//  Created by 看着太阳吃烧饼 on 15/7/17.
//  Copyright (c) 2015年 看着太阳吃烧饼. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieModal.h"
#import "MovieCell.h"
#define kwidth [UIScreen mainScreen].bounds.size.width
#define kheight [UIScreen mainScreen].bounds.size.height
@interface MovieViewController ()
{
    BOOL isLeft;
}
@end

@implementation MovieViewController
//加载电影的标题
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"电影";
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createNav];
    [self _createMovieTableView];
    [self _createPosterView];
    [self _initData];
    [self _loadData];
//    self.view.backgroundColor = [UIColor blueColor];
//    self.view.backgroundColor = [UIColor redColor];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - _create
- (void)_createNav {
    UIView *flipView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:flipView];
    self.navigationItem.rightBarButtonItem = item;
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn1 setImage:[UIImage imageNamed:@"list_home"] forState:UIControlStateNormal];
    [btn1 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    btn1.tag = 1;
    [btn1 sizeToFit];
    [btn1 addTarget:self action:@selector(flipAction:) forControlEvents:UIControlEventTouchUpInside];
    [flipView addSubview:btn1];
    //
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setImage:[UIImage imageNamed:@"poster_home"] forState:UIControlStateNormal];
    [btn2 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal] ;
    btn2.tag = 2;
    [btn2 sizeToFit];//设置btn2可以装的下所给图片
    [btn2 addTarget:self action:@selector(flipAction:) forControlEvents:UIControlEventTouchUpInside];
    [flipView addSubview:btn2];
    btn2.hidden = YES;
    
//    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:flipView];
//    self.navigationItem.rightBarButtonItem = item;

}

- (void)_createPosterView {
    _posterView = [[UIView alloc] initWithFrame:self.view.bounds];
    _posterView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_posterView];
}
- (void)_createMovieTableView {
//    self.view.backgroundColor = [UIColor blackColor];
    _movieTableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    _movieTableView.dataSource = self;
    _movieTableView.delegate = self;
    [self.view addSubview:_movieTableView];
    _movieTableView.hidden = YES;
    //注册cell
    UINib *nib = [UINib nibWithNibName:@"MovieCell" bundle:[NSBundle mainBundle]];
    [_movieTableView registerNib:nib forCellReuseIdentifier:@"cell"];
    
}

#pragma mark - action
- (void)flipAction:(UIButton *)button {
    UIView *flipView = self.navigationItem.rightBarButtonItem.customView;
    UIButton *btn1 = (UIButton *)[flipView viewWithTag:1];
    UIButton *btn2 = (UIButton *)[flipView viewWithTag:2];
    isLeft = !isLeft;
    [self _flipView:flipView isLeft:isLeft];
    [self _flipView:self.view isLeft:isLeft];
    btn1.hidden = !btn1.hidden;
    btn2.hidden = !btn2.hidden;
    _posterView.hidden = !_posterView.hidden;
    _movieTableView.hidden = !_movieTableView.hidden;

}
     
- (void)_flipView:(UIView *)view isLeft:(BOOL)isLeft {
    UIViewAnimationOptions option = isLeft?UIViewAnimationOptionTransitionFlipFromLeft:UIViewAnimationOptionTransitionFlipFromRight;
    [UIView transitionWithView:view duration:0.5 options:option animations:nil completion:nil];
}

#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _movieModalArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MovieCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//    if (cell == nil) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
//    }
    MovieModal *modal = _movieModalArray[indexPath.row];
//    cell.textLabel.text = modal.title;
    cell.modal = modal;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

#pragma mark - loaddata
- (void)_initData {
    _movieModalArray = [[NSMutableArray alloc] init];
}

- (void)_loadData {
    
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"us_box.json" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    NSArray *subjects = [dataDic objectForKey:@"subjects"];
    //提取数据
    for (NSDictionary * dic in subjects) {
        
        NSDictionary *subject = [dic objectForKey:@"subject"];
        MovieModal *modal = [[MovieModal alloc] init];
        modal.title = [subject objectForKey:@"title"];
        modal.year = [subject objectForKey:@"year"];
        modal.images = [subject objectForKey:@"images"];
        NSNumber *number = [[subject objectForKey:@"rating"] objectForKey:@"average"];
        modal.average = [number floatValue];
        [_movieModalArray addObject:modal];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
